﻿package base.helper;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import base.dao.CommonDao;

public class SpringHelper
{
	public static final String SPRING_CONFIG = "classpath*:spring_*.xml";
	private static ApplicationContext applicationContext;
	
	private static void initApplicationContext(ServletContext servletContext)
	{
		if(applicationContext == null && servletContext == null)
		{
			applicationContext = new ClassPathXmlApplicationContext(SPRING_CONFIG);
		}
		else if(applicationContext == null)
		{
			applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		}
	}
	
	public static Object getBean(String keyword, ServletContext servletContext)
	{
		initApplicationContext(servletContext);
		return applicationContext.getBean(keyword);
	}
	
	public static CommonDao getCommonDao(ServletContext servletContext)
	{
		return (CommonDao) getBean("commonDao", servletContext);
	}
}